/*
 * @(#)AdlTextAss.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.gea.ass;

import fr.ema.arch3d.ref.Fractal;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class AdlTextAss {

    /** Field description */
    private JTextPane textPane;

    /**
     * Constructs ...
     *
     */
    public AdlTextAss() {
        textPane = new JTextPane();

        // textPane.setBackground(Color.black);
        textPane.setEditable(false);
    }

    public void setBold(String str) {
        SimpleAttributeSet attrset = new SimpleAttributeSet();

        /*
         * StyleConstants.setForeground(attrset,Color.black);
         * StyleConstants.setUnderline(attrset,false);
         * StyleConstants.setItalic(attrset,false);
         */
        StyleConstants.setBold(attrset, true);

        // StyleConstants.setFontSize(attrset,22);
        insert(str, attrset);
    }

    public void insert(String str, AttributeSet attrset) {
        Document docs = textPane.getDocument();

        str = str;

        try {
            docs.insertString(docs.getLength(), str, attrset);
        } catch (BadLocationException ble) {
            System.out.println("BadLocationException:" + ble);
        }
    }

    public void insert(String str) {
        Document docs = textPane.getDocument();

        str = str;

        try {
            docs.insertString(docs.getLength(), str, null);
        } catch (BadLocationException ble) {
            System.out.println("BadLocationException:" + ble);
        }
    }

    public void changeLine() {
        Document docs = textPane.getDocument();
        String   str  = "\n";

        try {
            docs.insertString(docs.getLength(), str, null);
        } catch (BadLocationException ble) {
            System.out.println("BadLocationException:" + ble);
        }
    }

    public void addSpace() {
        Document docs = textPane.getDocument();
        String   str  = " ";

        try {
            docs.insertString(docs.getLength(), str, null);
        } catch (BadLocationException ble) {
            System.out.println("BadLocationException:" + ble);
        }
    }

    public java.awt.Component getComponent() {
        return textPane;
    }

    public Document createTextAss(Component container) {
        AdlTextAss pane = new AdlTextAss();

        // describe configuration name
        pane.setBold("assembly");
        pane.addSpace();

        try {
            pane.insert(Fractal.getAssemblyDescriptorController(container).getAssName());
        } catch (NoSuchInterfaceException e1) {

            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        pane.changeLine();
        pane.addSpace();
        pane.setBold("instance_of");

        // describe specification which this configuration implements
        pane.addSpace();

        try {
            pane.insert(Fractal.getAssemblyDescriptorController(container).getConfiguration().name);
        } catch (NoSuchInterfaceException e1) {

            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        pane.changeLine();

        List cmpIns_list = new ArrayList();
        List ctrIns_list = new ArrayList();

        try {
            cmpIns_list = Fractal.getAssemblyDescriptorController(container).getCmpsList();
            ctrIns_list = Fractal.getAssemblyDescriptorController(container).getCtrsList();

            // describe components
            pane.addSpace();
            pane.setBold("component_instances");
            pane.changeLine();

            for (int i = 0; i < cmpIns_list.size(); i++) {
                pane.addSpace();
                pane.addSpace();

                String a[] = (String[]) cmpIns_list.get(i);

                pane.insert(a[0]);

                // pane.setBold("instanciates");
                // pane.addSpace();
                // pane.insert(a[1]+";");
                pane.insert(";");
                pane.addSpace();

                if ((i > 0) && (i + 1) % 2 == 0) {
                    pane.changeLine();
                }

                // pane.changeLine();
            }

            pane.changeLine();

            // describe connectors

            /*
             * pane.addSpace();
             * pane.setBold("connectorInstances");
             * pane.changeLine();
             * for (int i = 0; i < ctrIns_list.size(); i++) {
             *       pane.addSpace();
             *       pane.addSpace();
             *       String a[]=(String[])ctrIns_list.get(i);
             *       pane.insert(a[0]);
             *       pane.addSpace();
             *       pane.setBold("implements");
             *       pane.addSpace();
             *       pane.insert(a[1]+";");
             *       pane.changeLine();
             * }
             */
        } catch (NoSuchInterfaceException e1) {

            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        pane.addSpace();
        pane.setBold("assembly_rules");
        pane.changeLine();
        pane.addSpace();
        pane.addSpace();
        pane.insert("Basket1.Company=\"EasyRentInc\"");

        // end describe configuration
        pane.changeLine();
        pane.setBold("end");
        pane.addSpace();

        try {
            pane.insert(Fractal.getAssemblyDescriptorController(container).getAssName());
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        pane.changeLine();

        Document docs = ((JTextPane) pane.getComponent()).getDocument();

        // System.out.println("hhhhhhhhhh");
        return docs;
    }

    /*
     * public Document createTextCmp(Component cmp){
     *         adlText pane=new adlText();
     *         //describe component ytpe
     *         pane.setBold("component");
     *         pane.addSpace();
     *         pane.insert(cmp.name);
     *         pane.changeLine();
     *
     *         //describe implement component type
     *         pane.addSpace();
     *         pane.addSpace();
     *         pane.setBold("implement");
     *         pane.addSpace();
     *         pane.insert(cmp.ctImpl.name);
     *         pane.changeLine();
     *
     *         //describe component implementation class
     *         pane.addSpace();
     *         pane.addSpace();
     *         pane.setBold("with_class");
     *         pane.addSpace();
     *         pane.insert(cmp.implClass);
     *         pane.changeLine();
     *
     *         //describe version information
     *         pane.addSpace();
     *         pane.addSpace();
     *         pane.setBold("version");
     *         pane.addSpace();
     *         pane.insert(cmp.v.version_name);
     *         pane.changeLine();
     *
     *         //end description
     *         pane.setBold("end");
     *         pane.addSpace();
     *         pane.insert(cmp.name);
     *
     *         Document docs=((JTextPane) pane.getComponent()).getDocument();
     *         return docs;
     *       }
     */

    /**
     *
     * @param cnt: the connection which need to describe
     * @return the document which describe the connection using ADL grammar
     */

    /*
     * public Document createTextCnt(Connector ctr){
     *         adlText pane=new adlText();
     *         //describe connector
     *         pane.setBold("connector");
     *         pane.addSpace();
     *         pane.insert(ctr.name);
     *         pane.changeLine();
     *
     *       //describe implement connection
     *         pane.addSpace();
     *         pane.addSpace();
     *         pane.setBold("implement");
     *         pane.addSpace();
     *         pane.insert(ctr.connection.name);
     *         pane.changeLine();
     *
     *         //describe connector implementation class
     *         pane.addSpace();
     *         pane.addSpace();
     *         pane.setBold("with_class");
     *         pane.addSpace();
     *         pane.insert(ctr.connectorImpl);
     *         pane.changeLine();
     *
     *
     *         //end description
     *         pane.setBold("end");
     *         pane.addSpace();
     *         pane.insert(ctr.name);
     *
     *         Document docs=((JTextPane) pane.getComponent()).getDocument();
     *         return docs;
     *       }
     */
}
